package com.atguigu.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.atguigu.common.constant.ProductConstant;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimall.product.entity.ProductAttrValueEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.ProductAttrValueService;
import com.atguigu.gulimall.product.vo.AttrVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrService;


/**
 * 商品属性
 *
 * @author zhangjianyou
 * @email 1844897920@qq.com
 * @date 2022-06-01 12:37:09
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Autowired
    private ProductAttrValueService productAttrValueService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    ///product/attr/update/{spuId}
    @PostMapping("/update/{spuId}")
    public R updateAttr(@RequestBody List<ProductAttrValueEntity> productAttrValueEntities,@PathVariable Long spuId){
        productAttrValueService.updateAttr(productAttrValueEntities,spuId);
        return R.ok();
    }

    ///product/attr/base/listforspu/{spuId}
    @RequestMapping("/base/listforspu/{spuId}")
    public R list(@PathVariable Long spuId){
        List<ProductAttrValueEntity> productAttrValueEntities = productAttrValueService.listforspu(spuId);
        return R.ok().put("data",productAttrValueEntities);
    }
    /**
     * 列表
     */
    @RequestMapping("/{type}/list/{catelogId}")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable Long catelogId,@PathVariable String type){
        //PageUtils page = attrService.queryPage(params);
        PageUtils page = attrService.queryList(params,catelogId,type);
        return R.ok().put("page", page);
    }


    @RequestMapping("/infoAttr/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R attrInfo(@PathVariable("attrId") Long attrId){
        AttrEntity attr = attrService.getById(attrId);
        return R.ok().setData(attr);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
		//AttrEntity attr = attrService.getById(attrId);
        AttrVo attrVo = attrService.getDetail(attrId);
        return R.ok().put("attr", attrVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @Transactional
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attrVo){
        AttrEntity attr = new AttrEntity();
        BeanUtils.copyProperties(attrVo,attr);
		attrService.save(attr);
		if(attrVo.getAttrType().equals(ProductConstant.AttrEnum.BASE_ATTR_VALUE.getCode())){
		    if(attrVo.getAttrGroupId()!=null){
                AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
                attrAttrgroupRelationEntity.setAttrId(attr.getAttrId());
                attrAttrgroupRelationEntity.setAttrGroupId(attrVo.getAttrGroupId());
                attrAttrgroupRelationService.save(attrAttrgroupRelationEntity);
            }
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @Transactional
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVo attr){
		attrService.updateById(attr);
        if(attr.getAttrType().equals(ProductConstant.AttrEnum.BASE_ATTR_VALUE.getCode())){
            AttrAttrgroupRelationEntity one = attrAttrgroupRelationService.getOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attr.getAttrId()
            ).eq("attr_group_id", attr.getAttrGroupId()));
            if(one!=null){
                attrAttrgroupRelationService.update(new UpdateWrapper<AttrAttrgroupRelationEntity>().eq("attr_id",attr.getAttrId())
                        .set("attr_group_id",attr.getAttrGroupId()));
            }else {
                AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
                attrAttrgroupRelationEntity.setAttrId(attr.getAttrId());
                attrAttrgroupRelationEntity.setAttrGroupId(attr.getAttrGroupId());
                attrAttrgroupRelationService.save(attrAttrgroupRelationEntity);
            }
        }
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
